<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    
	
	<title>_${customer.customerName}_执行上架任务</title>
	<script type="text/javascript">
	 
     var errorFlag=0;
    
	 $(function() {
         $("input[type=text]").each(function(){
            $(this).val('');
         });
         $("input[type=text]")[0].focus();
         
         $("input[type=button][name=checkInfo]").each(function(){
            $(this).attr("disabled",false);
            $(this).attr("class","btn btn-primary");
         });
         
         var status='${task.status}';
         if(status!='1'){
            $("input[type=text]").attr("readonly",true);
            $("input[type=button]").attr("disabled",true);
            $("input[type=button]").attr("class","btn");
         }
         
         errorFlag=0;
	 });
	
	 //美化库存信息
	 function formatStockInfo(sku) {
		 
		var temp = '';
		for ( var i = 0; i < carInfoArray.length; i++) {
			var car = carInfoArray[i];
			if (car == null || car == '') {
			} else {
				temp += "【" + car + "(件)】";
			}
		}
		return temp;
	 }
	 
	function stock(element){
        var id=$(element).attr("id").substring(6);
        var box_id="#box_"+id;
        $(box_id).focus();
    } 
	 
	function box(element){
        var id=$(element).attr("id").substring(4);
        var sku2_id="#sku2_"+id;
        $(sku2_id).focus();
    } 
    
    function sku(element){
       var id=$(element).attr("id").substring(5);
       var num2_id="#num2_"+id;
       $(num2_id).focus();
    }
    
    
    function check(element){
       
       var id=$(element).attr("id").substring(5);
       var sku1_id="#sku1_"+id;
       var num1_id="#num1_"+id;
       var stock_id="#stock_"+id;
       var box_id="#box_"+id;
       var sku2_id="#sku2_"+id;
       var num2_id="#num2_"+id;
       var chek_id="#chek_"+id;
       var tr_id="#tr_"+id;
       
       var sku1=$(sku1_id).html().trim();
       var sku2=$(sku2_id).val().trim();
       var stock=$(stock_id).val().trim();
       var box=$(box_id).val().trim();
       var num1=$(num1_id).html().trim();
       var num2=$(num2_id).val().trim();
       
       
       //验证库位编码
       if(stock == ""){
          $(stock_id).focus();
          $(stock_id).select();
          alert("请录入库位条码...");
          return;
       }
       
       //验证箱编码
       if(box == ""){
          $(box_id).focus();
          $(box_id).select();
          alert("请录入箱条码...");
          return;
       }
       
       //验证sku是否匹配
       if(sku2 ==""){
          $(sku2_id).focus();
          $(sku2_id).select();
          alert("请录入SKU编码...");
          return;
       }
       
       if(sku1!=sku2){
          $(sku2_id).focus();
          $(sku2_id).select();
          alert("sku编码有误，请检查核对...");
          return;
       }
       
       //验证数量是否匹配——开始
       if(num2==""){
          $(num2_id).focus();
          $(num2_id).select();
          alert("请录入数量......");
          return;
       }
       
       if(isNaN(num2)){ 
          alert("请输入数字！");
          $(num2_id).focus();
          $(num2_id).select();
          return;
       }
       
       if(num1!=num2){
          var r=confirm("实际入库数量异常，你确定上架吗？");
	      if(r==false){
    		      return;
	      }else{
            errorFlag=1;
	        alert("入库验证成功，请入库!");
	        $(chek_id).attr("disabled", true);
	        $(chek_id).attr("class", "btn");
	        $(area_id).attr("readonly",true);
	        $(sku2_id).attr("readonly",true);
	        $(num2_id).attr("readonly",true);
	        $(tr_id).css("background-color",'');
	      }
       }else{
            alert("入库验证成功，请入库!");
	        $(chek_id).attr("disabled", true);
	        $(chek_id).attr("class", "btn");
	        $(area_id).attr("readonly",true);
	        $(sku2_id).attr("readonly",true);
	        $(num2_id).attr("readonly",true);
	        $(tr_id).css("background-color",'');
       }
       
    }
   
   //创建库存信息对象
	function createStockInfo(stockCode,boxCode,sku,num,stockType,customerCode){
	    return {
	        stockCode:stockCode,
	        boxCode:boxCode,
	        sku:sku,
	        num:num,
	        stockType:stockType,
	        customerCode:customerCode
	    };
	}
	
	    
    function finish(){
      var flag=0;
      //上架信息
	  var taskInfo="";
	  //库存信息
	  var stockArray=new Array();
      
      //封装数据
      $("input[name='checkInfo']").each(function(){
         var temp=$(this).attr("class");
         var id=$(this).attr("id").substring(5);
         var tr_id="#tr_"+id;
         
         if(temp=='btn btn-primary'){
           $(tr_id).css("background-color",'yellow');
           flag=1;
         }else{
          
           $(tr_id).css("background-color",'');
           
           
	       var stock_id="#stock_"+id;
	       var box_id="#box_"+id;
	       var sku2_id="#sku2_"+id;
	       var num2_id="#num2_"+id;
	       var customerCode_id="#customerCode_"+id;
	       
	       
           
           var stockCode=$(stock_id).val();
           var boxCode=$(box_id).val();
           var sku=$(sku2_id).val();
	       var num=$(num2_id).val();
	       var customerCode=$(customerCode_id).val();
	       taskInfo+=sku+":"+num+";";
	       var stockInfo=createStockInfo(stockCode,boxCode,sku,num,"sales_num",customerCode);
	       
           stockArray.push(stockInfo);
         }
      });
      
      if(flag==1){
          alert("请完成所有商品入库操作！");
          return false;
      }
      
       //库存信息
	   var stockList = eval(stockArray); 
	   var stockText = JSON.stringify(stockList);
	   
       $.ajax({
			type:'post',
			url:'${ctx}/cusInstorageUpshelf/ajaxUpshelf',
			data:{
			    stockText:stockText,
				taskInfo:taskInfo,
				taskId:'${task.id}',
				errorFlag:errorFlag
			},
			dataType:'json',
			success:function(data) {
			    if(data==true){
			      alert("此任务已完成！");
			      
			      $("#finish_btn").attr("disabled", true);
			      $("#finish_btn").attr("class", "btn");
			      
			      $("#reload_btn").attr("disabled", true);
			      $("#reload_btn").attr("class", "btn");
			      
			    }else{
			      alert("网络繁忙，请稍后重试...");
			    }
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
	   });
      
      
      
    }
    
    </script>
</head>

<body>
	<div style="height: 10px;"></div>
	
	<table id="contentTable" class="table  table-bordered table-condensed">
	    <thead>
			<th style="vertical-align: middle; color: white;" colspan="9" bgcolor="#8EB4CB">入库信息</th>
		</thead>
		<thead><tr>
		    <th style="width: 80px;">任务批次号</th>
			<th style="width: 80px;">SKU编号</th>
			<th style="width: 80px;">数量 (件/箱)</th>
			<th style="width: 80px;">扫描库位编码</th>
			<th style="width: 80px;">扫描箱条码</th>
			<th style="width: 80px;">扫描sku编码</th>
			<th style="width: 80px;">实际数量 (件/箱)</th>
			<th style="width: 130px;">操作</th>
		</tr></thead>
		<tbody>
		
		<c:forEach items="${carInfoMap}" var="map" varStatus="v">
			<tr id="tr_${map.key}_${v.count}">
			   <td>${task.taskBatch}</td>
			   <td id="sku1_${map.key}_${v.count}">${map.key}</td>
			   <td id="num1_${map.key}_${v.count}">${map.value}</td>
			   <td><input type="text" id="stock_${map.key}_${v.count}" onchange="stock(this);"/></td>
			   <td><input type="text" id="box_${map.key}_${v.count}" onchange="box(this);"/></td>
			   <td><input type="text" id="sku2_${map.key}_${v.count}" onchange="sku(this);"/></td>
			   <td>
			     <input type="text" id="num2_${map.key}_${v.count}" />
			     <input type="hidden" id="customerCode_${map.key}_${v.count}" value="${customer.customerCode}"/>
			     <input type="hidden" id="sku_${map.key}_${v.count}" value="${map.key}"/>
			     <input type="hidden" id="info_${map.key}_${v.count}" value="${map.key}"/>
			   </td>
			   <td>
			      <input id="chek_${map.key}_${v.count}" class="btn btn-primary " type="button" name="checkInfo" value="入库验证" disabled="" onclick="check(this);"/>
			   </td>
			</tr>
			<tr  style="height: 30px; background-color: #4869A3; color: white;">
			     <td colspan="9">
			      <b>库存信息（箱位/库位/数量）:</b>
			      <c:forEach items="${stockInfoMap.get(map.key)}" var="stockInfo">
			                              【${stockInfo.boxCode} / ${stockInfo.stockCode} / ${stockInfo.salesNum}】
			      </c:forEach>
			     </td>
			</tr>
		</c:forEach>
		
		<tr>
		  <td colspan="9">
		     <input id="finish_btn" class="btn btn-primary" type="button" value="入库完成" onclick="finish();" />&nbsp;	
			 <input id="reload_btn" class="btn btn-danger" type="button" value="重新扫描入库" onclick="location.reload();" />&nbsp;
			 <a href="${ctx}/cusInstorageUpshelf" data-toggle="modal" class="btn btn-info" >返回</a>
		  </td>
		</tr>
		</tbody>
	</table>
	
	
</body>
</html>
